/*
 * $Id: ActRequest.java,v 1.32 2010/10/25 19:48:57 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.database.pojo;

import java.util.List;
import java.util.Vector;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.pojo.sysInfo.SystemInfo;
import com.ge.healthcare.autosc.common.util.TimeFormatStandard;

/**
 * ActRequest entity.
 * 
 * @author MyEclipse Persistence Tools
 */
public class ActRequest extends AbstractRequest implements java.io.Serializable {

	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = 7823636493770041894L;

	public static final String TABLE_NAME = "com.ge.healthcare.autosc.common.database.pojo.ActRequest";

	public static final String COL_REQUEST_ID = "requestId";
	public static final String COL_REQUEST_LIST_ID = "requestListId";
	public static final String COL_SYSTEM_ID = "systemId";
	public static final String COL_PORT_MASTER = "portMaster";
	public static final String COL_MODEM_NUMBER = "modemNum";
	public static final String COL_MODEM_AREA_CODE = "modemAreaCityCode";
	public static final String COL_SERVICE_CENTER = "serviceCenter";

	public static final String COL_ADDRESS_ID = "addressId";
	public static final String COL_PRODUCT_LINE = "productLine";
	public static final String COL_MODEL_TYPE = "modelType";
	public static final String COL_USN = "usn";
	public static final String COL_MODALITY = "modality";
	public static final String COL_MLN = "mln";
	public static final String COL_IP_ADDRESS = "ipAddress";
	public static final String COL_POLE = "pole";
	public static final String COL_ISO_CODE = "isoCode";
	public static final String COL_CONNECT_TYPE = "connectType";
	public static final String COL_REQUEST_DEFN_ID = "requestDefnId";
	public static final String COL_CODE_ID = "codeId";
	public static final String COL_START_TIME = "startTime";
	public static final String COL_END_TIME = "endTime";
	public static final String COL_SYSTEM_TYPE = "systemType";
	/*
	 * START These private fields are used as data storage for convenient
	 * access. It maintains no hibernate relationship
	 */
	private Vector<ActTask> actTasks = new Vector<ActTask>();
	private Vector<ActFileTransferDetails> actFileTransferDetails = new Vector<ActFileTransferDetails>();
	private CfgRequestDefn requestDefn = null;
	private String codeName = null;
	private boolean isEntitled = false;

	// END

	// Constructors

	/** default constructor */
	public ActRequest() {
	}

	public ActRequest(SystemInfo systemInfo) {
		this.setSystemId(systemInfo.getSystemId());
		this.setStartTime();
		this.setProductLine(systemInfo.getProductLine());
		this.setModelType(systemInfo.getModelType());
		this.setModality(systemInfo.getModality());
		this.setIpAddress(systemInfo.getIpAddress());
		this.setPole(systemInfo.getPole());
		this.setIsoCode(systemInfo.getIsoCode());
		this.setModemAreaCityCode(systemInfo.getModemAreaCityCode());
		this.setModemNum(systemInfo.getModemNum());
		this.setServiceCenter(systemInfo.getLocation());
		this.setUsn(systemInfo.getUsn());
		this.setMln(systemInfo.getMln());
		this.setAddressId(systemInfo.getAddressId());
		this.setPortMaster(systemInfo.getPortMaster());
		this.setServiceCenter(systemInfo.getServiceCenter());
		this.setConnectType(systemInfo.getConnectType());
	}

	public boolean equals(ActRequest request) {
		if (this.getRequestId().equals(request.getRequestId())
				&& this.getRequestListId().equals(request.getRequestListId())
				&& this.getSystemId().equals(request.getSystemId())) {
			return true;
		} else {
			return false;
		}
	}

	public String toString() {
		StringBuffer str = new StringBuffer();
		str = str.append("{requestId=").append(this.getRequestId())
				.append("},");
		str = str.append("{requestDefnId=").append(this.getRequestDefnId())
				.append("},");
		str = str.append("{requestListId=").append(this.getRequestListId())
				.append("},");
		str = str.append("{codeId=").append(this.getCodeId()).append("},");
		str = str.append("{systemId=").append(this.getSystemId()).append("},");
		str = str.append("usn=").append(this.getUsn()).append("},");
		str = str.append("mln=").append(this.getMln()).append("},");
		str = str.append("{productLine=").append(this.getProductLine()).append(
				"},");
		str = str.append("{modelType=").append(this.getModelType())
				.append("},");
		str = str.append("{modality=").append(this.getModality()).append("},");
		str = str.append("{ipAddress=").append(this.getIpAddress())
				.append("},");
		str = str.append("{systemType=").append(this.getSystemType())
		.append("},");
		str = str.append("{pole=").append(this.getPole()).append("},");
		str = str.append("{isoCode=").append(this.getIsoCode()).append("},");
		str = str.append("{modemAreaCityCode=").append(
				this.getModemAreaCityCode()).append("},");
		str = str.append("{modemNum=").append(this.getModemNum()).append("},");
		str = str.append("{connectType=").append(this.getConnectType()).append(
				"},");
		str = str.append("{addressId=").append(this.getAddressId())
				.append("},");
		str = str.append("{portMaster=").append(this.getPortMaster()).append(
				"},");
		str = str.append("{serviceCenter=").append(this.getServiceCenter())
				.append("},");
		str = str.append("{Start Time=").append(
				TimeFormatStandard.getDtInEndUserDisplayFormat(this
						.getStartTime())).append("},");
		str = str.append("{End Time=").append(
				TimeFormatStandard.getDtInEndUserDisplayFormat(this
						.getEndTime())).append("}");
		return str.toString();
	}

	public Long getObjectId() {
		return this.getRequestId();
	}

	public String getObjectType() {
		return HstStepTime.REQUEST_TYPE;
	}

	public void setRequestCode(ICodeDetailsDAO iCodeDetailsDAO, String codeName) {
		CfgCodeDetails codeDetails = (CfgCodeDetails) iCodeDetailsDAO
				.getUniqueCodedetailsByCodeName(codeName);
		if (codeDetails != null) {
			setCodeId(codeDetails.getCodeId());
			setCodeName(codeName);
		} else {
			throw new RuntimeException(
					"Unable to set request code because code is not found "
							+ "with code name " + codeName);
		}
	}

	public boolean isValid() {
		if (ASCUtil.isNullOrEmptyObject(this.getRequestId())
				|| ASCUtil.isNullOrEmptyObject(this.getRequestListId())
				|| ASCUtil.isNullOrEmptyObject(this.getCodeId())
				|| ASCUtil.isNullOrEmptyObject(this.getSystemId())) {
			return false;
		}
		return true;
	}

	public void setEndTime() {
		this.setEndTime(TimeFormatStandard.getNowMillis());
	}

	public void setStartTime() {
		this.setStartTime(TimeFormatStandard.getNowMillis());

	}

	public void addActTask(ActTask actTask) {
		actTasks.add(actTask);
	}

	public List<ActTask> getActTasks() {
		return actTasks;
	}

	public ActTask getTask(int idx) {
		ActTask task = null;
		if (!ASCUtil.isNullOrEmptyList(actTasks)) {
			if (idx >= 0 && idx < actTasks.size()) {
				task = actTasks.get(idx);
			}
		}
		return task;
	}

	public void addActFileTransferDetails(
			ActFileTransferDetails actFileTransferDetail) {
		this.actFileTransferDetails.add(actFileTransferDetail);
	}

	public List<ActFileTransferDetails> getActFileTransferDetails() {
		return actFileTransferDetails;
	}

	public void setRequestDefn(CfgRequestDefn requestDefn) {
		this.requestDefn = requestDefn;
	}

	public CfgRequestDefn getRequestDefn() {
		return requestDefn;
	}

	public boolean isTask() {
		return false;
	}

	public boolean isSystemMatch(SystemInfo systemInfo) {
		boolean owner = false;
		String ipAddress = systemInfo.getIpAddress();
		String usn = systemInfo.getUsn();
		if ((ipAddress != null) && (usn != null)) {
			if ((ipAddress.equals(this.getIpAddress()))
					&& (usn.equals(this.getUsn()))) {
				owner = true;
			}
		}
		return owner;
	}

	public void updateSystemInfo(SystemInfo systemInfo) {
		String methodName = "updateSystemInfo";
		if (ASCUtil.isNullOrEmptyObject(systemInfo)) {
			ASCLogger.warn(this.getClass(), methodName,
					"Null system info passed, initialization to return null");
			return;
		}
		this.setConnectType(systemInfo.getConnectType());
		this.setIpAddress(systemInfo.getIpAddress());
		this.setIsoCode(systemInfo.getIsoCode());
		this.setModality(systemInfo.getModality());
		this.setModelType(systemInfo.getModelType());
		this.setModemAreaCityCode(systemInfo.getModemAreaCityCode());
		this.setModemNum(systemInfo.getModemNum());
		this.setPole(systemInfo.getPole());
		this.setPortMaster(systemInfo.getPortMaster());
		this.setProductLine(systemInfo.getProductLine());
		this.setServiceCenter(systemInfo.getServiceCenter());
		this.setSystemId(systemInfo.getSystemId());
		this.setUsn(systemInfo.getUsn());
		this.setMln(systemInfo.getMln());
		this.setAddressId(systemInfo.getAddressId());
		this.setSystemType(systemInfo.getSystemType());
	}

	/**
	 * @return the codeName
	 */
	public String getCodeName() {
		return codeName;
	}

	/**
	 * @param codeName
	 *            the codeName to set
	 */
	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}

	/**
	 * @return the isEntitled
	 */
	public boolean isEntitled() {
		return isEntitled;
	}

	/**
	 * @param isEntitled
	 *            the isEntitled to set
	 */
	public void setEntitled(boolean isEntitled) {
		this.isEntitled = isEntitled;
	}
}
